package com.gw.vip.server.service.dao_service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gw.vip.common.constants.VipConstants;
import com.gw.vip.common.exception.ExceptionEnums;
import com.gw.vip.common.exception.ServiceException;
import com.gw.vip.server.constants.SysArgKey;
import com.gw.vip.server.dao.SysArgMapper;
import com.gw.vip.server.po.SysArg;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @author GW
 * @description
 * @since 2022-05-04
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class SysArgService extends ServiceImpl<SysArgMapper, SysArg> {

    private final SysArgMapper mapper;

    /**
     * 获取当前清算日期
     *
     * @return
     */
    public LocalDate getClearDate() {
        String currClearDateStr = this.mapper.valueById(SysArgKey.CURR_CLEAR_DATE);
        if (StringUtils.isBlank(currClearDateStr)) {
            log.error("系统参数【{}】当前清算日期不存在！", SysArgKey.CURR_CLEAR_DATE);
            throw new ServiceException(ExceptionEnums.ITEM_NOT_EXIST, "系统参数，当前日期不存在！");
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return LocalDate.parse(currClearDateStr, df);
    }

    /**
     * 获取当前清算日期截止时间点
     *
     * @return
     */
    public LocalDateTime getClearMaxTime() {
        return LocalDateTime.of(this.getClearDate(), LocalTime.MIN).plusDays(1);
    }

    public boolean needClear(LocalDateTime txnDateTime) {
        LocalDateTime clearMaxTime = this.getClearMaxTime();
        return clearMaxTime.compareTo(txnDateTime) >= 0;
    }

    public void setNextClearDate(LocalDate clearDate) {
        LocalDate nextClearDate = LocalDate.now();
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String nextClearDateStr = df.format(nextClearDate);

        //更新系统参数
        int i = this.mapper.updateValue(SysArgKey.CURR_CLEAR_DATE, nextClearDateStr);
        if (i!=1){
            log.error("系统参数【{}】更新失败！", SysArgKey.CURR_CLEAR_DATE);
            throw new ServiceException("系统参数更新失败！");
        }
    }

    public <T> T getObject(String id, Class<T> clazz) {
        String argValue = this.mapper.valueById(id);
        if (StringUtils.isBlank(argValue)){
            log.warn("配置不存在！id【{}】", id);
            return null;
        }
        JSONObject argJson = (JSONObject) JSONObject.parse(argValue);
        return JSONObject.toJavaObject(argJson, clazz);
    }

    public void setObject(String id, String argName, Object object, String memo){
        if (StringUtils.isBlank(id)){
            log.error("id为空！");
            throw new ServiceException("id为空！");
        }

        SysArg sysArg = this.getById(id);
        if (sysArg!=null){
            log.error("配置已经存在！");
            throw new ServiceException("配置已经存在!");
        }

        if (object == null){
            log.error("object为空！");
            throw new ServiceException("object为空!");
        }

        String argValue = JSONObject.toJSONString(object);

        sysArg = new SysArg();
        sysArg.setId(id);
        sysArg.setArgName(argName);
        sysArg.setArgValue(argValue);
        sysArg.setMemo(memo);
        sysArg.setCrtTime(LocalDateTime.now());
        sysArg.setCrtName(VipConstants.SYS);
        sysArg.setUpdTime(sysArg.getCrtTime());
        sysArg.setUpdName(sysArg.getCrtName());
        this.save(sysArg);
    }
}
